package com.libs.modbus;

import android.content.Context;


import com.libs.modbus.exception.PlcException;
import com.libs.modbus.interfaces.OnInitListener;

import java.io.File;
import java.io.IOException;

import android_serialport_api.SerialPort;


/**
 * Created by lny on 2018/4/23.
 */

public class MbManager {

    public static final String TAG = "MbManager";

    public static MbManager instance;
    private Context mContext;
    private OnInitListener mListener;

    private static final int busid = 8;
    private static final int nb = 1;

    private ModbusMaster master;


    private static boolean inited;


    public static MbManager getInstance(){
        if (instance==null){
            synchronized (MbManager.class){
                if (instance==null){
                    instance = new MbManager();
                }
            }
        }
        return instance;
    }

    public static boolean isInited(){
        return inited;
    }

    public void init(Context context, final OnInitListener listener) {
        mContext = context;
        mListener = listener;
        try {
            master = new ModbusMaster(new SerialPort(new File("/dev/ttyS1"), 9600));
            if (listener != null) {
                listener.success();
            }
        } catch (IOException e) {
            e.printStackTrace();
            listener.error(-1);
        }
        inited = true;
    }
    public int read_registers_must(int addr){
        while (true){
            try {
                return read_registers(addr);
            } catch (PlcException e) {
                e.printStackTrace();
            }
        }
    }

    public int read_registers32_must(int addr){
        while (true){
            try {
                return read_registers32(addr);
            } catch (PlcException e) {
                e.printStackTrace();
            }
        }
    }

    public int  read_registers(final int addr)  throws PlcException {
        try {
            int value = master.readHoldingRegister(busid, addr);
            return value;
        } catch (Exception e) {
            e.printStackTrace();
            throw new PlcException(e.getMessage());
        }
    }

    public int  read_registers32(final int addr)  throws PlcException {
        try {
            int value = master.readHoldingRegister32(busid, addr);
            return value;
        } catch (Exception e) {
            e.printStackTrace();
            throw new PlcException(e.getMessage());
        }

    }


    public int[]  read_registers(final int addr,int numberOfPoints)  throws PlcException {
        try {
            int[] values = master.readHoldingRegisters(busid, addr,numberOfPoints);
            return values;
        } catch (Exception e) {
            e.printStackTrace();
            throw new PlcException(e.getMessage());
        }
    }

    public void write_registers_must(final int addr, final int value){
        while (true){
            try {
                write_registers(addr,value);
                break;
            } catch (PlcException e) {
                e.printStackTrace();
            }
        }
    }

    public void write_registers32_must(final int addr, final int value){
        while (true){
            try {
                write_registers32(addr,value);
                break;
            } catch (PlcException e) {
                e.printStackTrace();
            }
        }
    }







    public int write_registers(final int addr, final int value) throws PlcException{
//        int code = modbusApi.modbus_write_register(busid, addr, value);
//        Log.d(TAG,"read_registers :addr="+addr+" , value="+value+" , code="+code);
//        if (code<0){
//            SystemClock.sleep(500);
//            throw new PlcException("modbus_read_registers addr = "+addr +" value = "+value+" code = "+code);
//        }
//        return code;

        try {
            master.writeSingleRegister(busid,addr,value);
            return 1;
        } catch (Exception e) {
            e.printStackTrace();
            throw new PlcException(e.getMessage());
        }
    }

    public int write_registers32(final int addr, final int value) throws PlcException{
//        int code = modbusApi.modbus_write_register(busid, addr, value);
//        Log.d(TAG,"read_registers :addr="+addr+" , value="+value+" , code="+code);
//        if (code<0){
//            SystemClock.sleep(500);
//            throw new PlcException("modbus_read_registers addr = "+addr +" value = "+value+" code = "+code);
//        }
//        return code;

        try {
            master.writeSingleRegister32(busid,addr,value);
            return 1;
        } catch (Exception e) {
            e.printStackTrace();
            throw new PlcException(e.getMessage());
        }
    }

    public void reset_registers(int addr){
        write_registers_must(addr,0);
    }
}
